<template>
  <!-- 采购订单审核 -->
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="请购单号">
              <a-input placeholder="请输入请购单号" v-model="queryParam.serial"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="客户">
              <j-search-select-tag placeholder="请选择客户" v-model="queryParam.customId" dict="hf_custom,name,id" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="6">
            <a-form-item label="客户订单号">
              <a-input v-model="queryParam.customOrderSerial" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="6">
            <a-form-item label="关联销售订单号">
              <a-input type="list" v-model="queryParam.sourceSerial" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="6">
            <a-form-item label="单据日期">
              <j-date type="list" v-model="queryParam.documentTime" style="width: 100%" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
      <a-button type="primary" @click="searchReset" icon="reload">重置</a-button>
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('采购订单')">导出</a-button>
      <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        @change="handleImportExcel"
      >
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
        :customRow="clickThenSelect"
        @change="handleTableChange"
      >
        <template slot="isClosed" slot-scope="text">
          <a-tag :color="text === '未关闭' ? 'red' : 'green'">{{ text }}</a-tag>
        </template>
        <template slot="status" slot-scope="text">
          <a-tag :color="text === '未审核' ? 'red' : 'green'">{{ text }}</a-tag>
        </template>

        <template slot="generateAction" slot-scope="text, record">
          <a @click="handleGenerateInput(record)">生成采购入库订单</a>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-if="record.status === 0">编辑</a>
          <a-divider type="vertical" v-if="record.status === 0" />
          <a @click="handleDelete(record.id)" v-if="record.status === 0">删除</a>
          <a-divider type="vertical" v-if="record.status === 0" />
          <a @click="handleUndoCheck(record)" v-if="record.status === 1">反审核</a>
          <a-divider type="vertical" v-if="record.status === 1" />
          <a @click="handleCheck(record)" v-if="record.status === 0">审核</a>
          <a-divider type="vertical" v-if="record.status === 0" />
          <a @click="handleClose(record)" v-if="record.isClosed === 0">关闭</a>
          <a @click="handleUndoClose(record)" v-if="record.isClosed === 1">反关闭</a>
        </span>
      </a-table>
    </div>

    <a-tabs defaultActiveKey="1">
      <a-tab-pane tab="采购订单详情" key="1">
        <HfPurchaseorderDetailList
          :mainId="selectedMainId"
          :sourceSerial="sourceSerial"
          :supplier-id="supplierId"
          :supplier-order-serial="supplierOrderSerial"
          ref="HfPurchaseorderDetailList"
        />
      </a-tab-pane>
    </a-tabs>

    <hfPurchaseOrder-modal ref="modalForm" @ok="modalFormOk"></hfPurchaseOrder-modal>
    <GenerateInputModal ref="generateInputModal" @ok="modalFormOk" :sourceId="selectedMainId" />
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import HfPurchaseOrderModal from './modules/HfPurchaseOrderModal'
import { getAction, postAction } from '@/api/manage'
import HfPurchaseorderDetailList from './HfPurchaseorderDetailList'
import '@/assets/less/TableExpand.less'
import store from '@/store'
import dayjs from 'dayjs'
import GenerateInputModal from '@views/purchase/modules/GenerateInputModal.vue'

export default {
  name: 'HfPurchaseOrderCheckList',
  mixins: [JeecgListMixin],
  components: {
    GenerateInputModal,
    HfPurchaseorderDetailList,
    HfPurchaseOrderModal
  },
  data() {
    return {
      isorter: {
        column: 'createTime',
        order: 'desc'
      },
      description: '采购订单管理页面',
      // 表头
      columns: [
        {
          title: '订单编号',
          align: 'center',
          dataIndex: 'serial'
        },
        {
          title: '关联销售订单号',
          align: 'center',
          dataIndex: 'sourceSerial'
        },
        {
          title: '供应商订单号',
          align: 'center',
          dataIndex: 'supplierOrderSerial'
        },
        {
          title: '供应商',
          align: 'center',
          dataIndex: 'supplierId_dictText'
        },
        {
          title: '单据日期',
          align: 'center',
          dataIndex: 'documentDate'
        },
        {
          title: '结算日期',
          align: 'center',
          dataIndex: 'paymentTime'
        },
        {
          title: '交货日期',
          align: 'center',
          dataIndex: 'deliveryTime'
        },
        {
          title: '制单',
          align: 'center',
          dataIndex: 'operator'
        },
        {
          title: '审核员',
          align: 'center',
          dataIndex: 'checker'
        },
        {
          title: '审核时间',
          align: 'center',
          dataIndex: 'checkTime'
        },
        {
          title: '审核状态',
          align: 'center',
          dataIndex: 'status_dictText',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '关闭状态',
          align: 'center',
          dataIndex: 'isClosed_dictText',
          scopedSlots: { customRender: 'isClosed' }
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark'
        },
        // {
        //   title: '生成操作',
        //   dataIndex: 'generateAction',
        //   align: 'center',
        //   fixed: 'right',
        //   width: 147,
        //   scopedSlots: { customRender: 'generateAction' }
        // },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/purchase/hfPurchaseOrder/checkList',
        delete: '/purchase/hfPurchaseOrder/delete',
        deleteBatch: '/purchase/hfPurchaseOrder/deleteBatch',
        exportXlsUrl: '/purchase/hfPurchaseOrder/exportXls',
        importExcelUrl: 'purchase/hfPurchaseOrder/importExcel',
        check: '/purchase/hfPurchaseOrder/check',
        undoCheck: '/purchase/hfPurchaseOrder/undoCheck',
        close: '/purchase/hfPurchaseOrder/close',
        undoClose: '/purchase/hfPurchaseOrder/undoClose'
      },
      dictOptions: {
        supplier: [],
        purchaseType: [],
        payType: []
      },
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 5,
        pageSizeOptions: ['5', '10', '50'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      selectedMainId: '',
      sourceSerial: '',
      superFieldList: [],
      superQueryFieldList: [],
      superQueryFlag: false,
      superQueryParams: '',
      supplierOrderSerial: '',
      supplierId: ''
    }
  },
  created() {},
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    initDictConfig() {},
    clickThenSelect(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(record.id.split(','), [record])
          }
        }
      }
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.selectedMainId = ''
      this.sourceSerial = ''
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedMainId = selectedRowKeys[0]
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
      this.supplierOrderSerial = selectionRows[0].supplierOrderSerial
      this.supplierId = selectionRows[0].supplierId
      this.$refs.HfPurchaseorderDetailList.isChecked = true
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      this.onClearSelected()
      var params = this.getQueryParams() //查询条件
      this.loading = true
      getAction(this.url.list, params).then(res => {
        if (res.success) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    handleGenerateInput(record) {
      this.$refs.generateInputModal.visible = true
      this.$refs.generateInputModal.loadData1(record.id)
      this.$refs.generateInputModal.model = record
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
